package frame.system;

import frame.AppConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;

@RestController
public class  HelloController {



	private DateFormat df;
	private String startTime;

	/** */
	@Autowired
	private AppConfiguration appConfiguration;

	@Resource(name="df1")
	public void setDf(DateFormat df) {
		this.df = df;
	}

	@Resource(name="startTime")
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}


	public String say() {
		return "Hello World!";
	}

	@RequestMapping("/now")
	public String now() {
		return df.format(new Date());
	}
	@RequestMapping("/startTime")
	public String startTime() {
		return "The app was started at " + startTime;
	}
/*	@RequestMapping("/err")
	public String error(HttpServletResponse response) throws IOException {
	    if(new Random().nextInt(10) > 6)
	        response.sendError(500, "服务器错误信息");
	    return "err";
    }*/
    @RequestMapping("/errors")
    public String error() {
		return "权限不足，请联系管理员";
	}

    @RequestMapping("/set1")
    public String sets(HttpSession session,String a) {
		System.out.println("a = " + a);
		session.setAttribute("a",a);
		return a;
	}

	@RequestMapping("/gets")
	public String gets(HttpSession session) {
		Object a = session.getAttribute("a");
		String b =  a== null?"-":a + "b";
		System.out.println("b = " + b);
		ServletContext context = session.getServletContext();
		context.setAttribute("b",b);
		return b;
	}


	@RequestMapping("/reader")
	public String reader(@RequestParam("content") String content, @RequestParam("title") String title) {
		System.out.println(title);
		System.out.println(content);
		return "success";
	}

	@RequestMapping("/rem")
	public static String rem(HttpSession session) {
		String a = session.getAttribute("a") == null?"-":"a";
		System.out.println("a = " + a);
		session.removeAttribute("a");
		return a;
	}
	@RequestMapping("/js")
	public String jsVersion(){
		return appConfiguration.getJsVersion();
	}
}
